package com.eric.common.file;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Downloader {
	public boolean Download(String httpUrl,String saveFile){
		
	    // 下载网络文件
	    int byteread = 0;
	
	    URL url = null;
		try {
			url = new URL(httpUrl);
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
			return false;
		}
	
	    try {
	        URLConnection conn = url.openConnection();
	        InputStream inStream = conn.getInputStream();
	        FileOutputStream fos = new FileOutputStream(saveFile);
	
	        byte[] buffer = new byte[1024];
	        while ((byteread = inStream.read(buffer)) != -1) {
	            fos.write(buffer, 0, byteread);
	        }
	        fos.close();
	        return true;
	    } catch (FileNotFoundException e) {
	        e.printStackTrace();
	        return false;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return false;
	    }
	}
}
